/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesis2.pucp.Envio.DAO;

import com.tesis2.pucp.Modelo.TesisHorario;
import com.tesis2.pucp.Seguridad.DAO.*;
import com.tesis2.pucp.Modelo.TesisPerfil;
import com.tesis2.pucp.Modelo.TesisUsuario;
import com.tesis2.pucp.Modelo.TesisVehiculo;
import com.tesis2.pucp.Util.HibernateUtil;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author pachon
 */
public class VehiculoDAO {
    
    public void guardarVehiculo(TesisVehiculo vehiculo){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx =  session.beginTransaction();
        try {
            session.saveOrUpdate(vehiculo);
            tx.commit();
        }catch(HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();            
        }        
    }
    
    public ArrayList<String> listarEstado() {
        ArrayList<String> estados = new ArrayList<>();
        estados.add("Activo");
        estados.add("Inactivo");
        estados.add("Inhabilitado");
        return estados;
    }
    
    public ArrayList<TesisHorario> listarHorario(){
        ArrayList<TesisHorario> listaHorarios = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        
        List lista = session.createQuery("from TesisHorario order by idhorario").list();
        Iterator iter = lista.iterator();
        while (iter.hasNext()) {    
            TesisHorario a = (TesisHorario) iter.next();        
            listaHorarios.add(a);
        }
        
        tx.commit();
        session.close();

        return listaHorarios;
    }
    
    public ArrayList<TesisVehiculo> listarVehiculos()
    {
        ArrayList<TesisVehiculo> listaResultados = new ArrayList<>();       
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from TesisVehiculo order by idvehiculo").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                TesisVehiculo p = (TesisVehiculo) iter.next();
                Hibernate.initialize(p.getAlto());
                Hibernate.initialize(p.getAncho());
                Hibernate.initialize(p.getDescripcion());
                Hibernate.initialize(p.getEstado());
                Hibernate.initialize(p.getIdvehiculo());
                Hibernate.initialize(p.getLargo());
                Hibernate.initialize(p.getTesisHorario());
                listaResultados.add(p);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return listaResultados;
        }         
    }    
    
}
